<template>
  <el-dialog
    :title="!dataForm.id ? '添加导师' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="导师编码" prop="userId">
      <el-input v-model="dataForm.tutorId" placeholder="导师编码" :rules="dataRule"></el-input>
    </el-form-item>
      <el-form-item label="所属学院" prop="collegeName">
        <el-input v-model="dataForm.collegeName" v-popover:collegeListPopover :readonly="true" placeholder="点击选择所属学院" class="college-list__input"></el-input>
        <el-select-dropdown
          ref="collegeListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="collegeList"
            :props="collegeListTreeProps"
            node-key="collegeId"
            ref="collegeListTree"
            @current-change="collegeListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-select-dropdown>
      </el-form-item>
      <el-form-item label="实验室" prop="laboratoryName">
        <el-select-dropdown
          ref="collegeListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="laboratoryList"
            :props="laboratoryListTreeProps"
            node-key="laboratoryId"
            ref="collegeListTree"
            @current-change="laboratoryListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-select-dropdown>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        collegeList: [],
        laboratoryList: [],
        collegeListTreeProps: {
          label: 'name',
          children: 'children'
        },
        pageIndex: 1,
        pageSize: 10,
        laboratoryListTreeProps: {
          label: 'name',
          children: 'children'
        },
        dataForm: {
          tutorId: '',
          collegeId: 0,
          collegeName: '',
          laboratoryId: 0,
          laboratoryName: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '导师编码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tutorId = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tutorId) {
            this.$http({
              url: this.$http.adornUrl(`/sys/college/select`),
              method: 'get',
              params: this.$http.adornParams({})
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.collegeList = treeDataTranslate(data.collegeList, 'collegeId')
              }
            }).then(() => {
              this.$http({
                url: this.$http.adornUrl(`/sys/laboratory/select`),
                method: 'get',
                params: this.$http.adornParams({
                  'page': this.pageIndex,
                  'limit': this.pageSize
                })
              }).then(({data}) => {
                if (data && data.code === 0){
                  this.laboratoryList = treeDataTranslate(data.laboratoryList, 'laboratoryId')
                }
              })
            })
          }
        })
      },
      // 菜单树选中
      collegeListTreeCurrentChangeHandle (data, node) {
        this.dataForm.collegeId = data.collegeId
        this.dataForm.collegeName = data.name
      },
      laboratoryListTreeCurrentChangeHandle (data, node) {
        this.dataForm.laboratoryId = data.laboratoryId
        this.dataForm.laboratoryName = data.laboratoryName
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/tutor/save`),
              method: 'post',
              data: this.$http.adornData({
                'tutorId': this.dataForm.id || undefined,
                'userId': this.dataForm.tutorId,
                'collegeId': this.dataForm.collegeId,
                'labId': this.dataForm.laboratoryId,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
